package com.starnet.yarnmonitor.yarn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starnet.yarnmonitor.yarn.entity.FilesInHDFS;
import com.starnet.yarnmonitor.yarn.service.HDFSService;
import com.starnet.yarnmonitor.yarn.utils.PageUtils;
import com.starnet.yarnmonitor.yarn.vo.result.PageResult;
import com.starnet.yarnmonitor.yarn.vo.result.PageVO;
import com.starnet.yarnmonitor.yarn.vo.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/hdfs/file")
public class HDFSController {

    @Resource
    HDFSService hdfsService;


    @ApiOperation(value = "上传文件")
    @PostMapping
    public Result uploadFile(@RequestPart MultipartFile multipartFile, @RequestParam(value = "path", required = false) String path) throws IOException {
        hdfsService.uploadFile(multipartFile, path);
        return Result.success();
    }

    @ApiOperation(value = "下载文件")
    @GetMapping
    public void downloadFile(@RequestParam(value = "id", required = true) Long id, HttpServletResponse response) throws IOException {
        hdfsService.downloadFile(id, response);
        return;
    }

    @ApiOperation(value = "获取当前用户已上传文件列表")
    @GetMapping("/list")
    public PageResult listYourFiles(@RequestParam(value = "cur", required = false) Integer cur, @RequestParam(value = "size", required = false) Integer size) {
        IPage<FilesInHDFS> all = hdfsService.listYourFiles(cur, size);
        PageVO pagevo = PageUtils.getPageVOFromIPage(all);
        return PageResult.success(all.getRecords(), pagevo);
    }

    @ApiOperation(value = "超管专用，获取所有已上传文件列表")
    @GetMapping("/list/all")
    public PageResult listAllFiles(@RequestParam(value = "cur", required = false) Integer cur, @RequestParam(value = "size", required = false) Integer size) {
        IPage<FilesInHDFS> all = hdfsService.listAllFiles(cur, size);
        PageVO pagevo = PageUtils.getPageVOFromIPage(all);
        return PageResult.success(all.getRecords(), pagevo);
    }

    @ApiOperation(value = "根据id删除文件")
    @DeleteMapping
    public Result deleteFileById(@RequestParam(value = "id", required = true) Long id) throws IOException {
        hdfsService.deleteFileById(id);
        return Result.success();
    }


}
